<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>API 请求次数与日志查询</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            background-color: #f9fdf9;
            color: #333;
        }
        .container {
            max-width: 800px;
            margin: 40px auto;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            border: 1px solid #e0e0e0;
        }
        h1, h2 {
            text-align: center;
            color: #4CAF50; /* 优雅的绿色 */
        }
        .result {
            font-size: 1.2em;
            margin-top: 20px;
            text-align: center;
            color: #555; /* 深灰色 */
        }
        .error {
            color: red;
            font-size: 1.2em;
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            table-layout: fixed;
        }
        table, th, td {
            border: 1px solid #ccc;
        }
        th, td {
            padding: 12px;
            text-align: left;
            overflow-wrap: break-word;
            word-wrap: break-word;
            word-break: break-all;
        }
        th {
            background-color: #d5eddf; /* 浅绿色背景 */
            color: #333; /* 深色表头文字 */
        }
        tr:hover {
            background-color: #f0fff0; /* 行悬停效果 */
        }
        .log-container {
            margin-top: 30px;
        }
        .footer {
            text-align: center;
            margin-top: 30px;
            font-size: 0.9em;
            color: #777;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>查询当天API请求日志</h1>
        <div class="result">
            <?php
            // 数据库连接参数
            $host = 'localhost';
            $username = 'b7wvjkvc8';  // 替换为实际的数据库用户名
            $password = 'ny5cw568';  // 替换为实际的数据库密码
            $database = 'b7wvjkvc8';  // 替换为实际的数据库名称

            // 连接数据库
            $conn = new mysqli($host, $username, $password, $database);

            // 检查连接是否成功
            if ($conn->connect_error) {
                die("<p class='error'>数据库连接失败: " . $conn->connect_error . "</p>");
            }

            // 获取今天的日期
            $today = date('Y-m-d');

            // 查询当天的请求次数
            $stmt = $conn->prepare("SELECT request_count FROM api_requests WHERE request_date = ?");
            $stmt->bind_param('s', $today);
            $stmt->execute();
            $stmt->bind_result($requestCount);
            $stmt->fetch();
            $stmt->close();

            // 显示请求次数
            if ($requestCount !== null) {
                echo "<p>今天已经使用了 <strong>$requestCount</strong> 次 API 请求</p>";
            } else {
                echo "<p>今天还没有使用 API 请求</p>";
            }

            // 查询当天的请求日志
            $stmt = $conn->prepare("SELECT ip_address, request_time, request_url, content_type, response_status FROM api_request_log WHERE DATE(request_time) = ?");
            $stmt->bind_param('s', $today);
            $stmt->execute();
            $result = $stmt->get_result();

            // 检查是否有日志
            if ($result->num_rows > 0) {
                echo "<div class='log-container'>";
                echo "<h2>请求日志详情</h2>";
                echo "<table>";
                echo "<tr><th>IP 地址</th><th>请求时间</th><th>请求 URL</th><th>内容类型</th><th>响应状态</th></tr>";

                // 输出每条日志
                while ($row = $result->fetch_assoc()) {
                    $ipAddress = $row['ip_address'];
                    $requestTime = $row['request_time'];
                    $requestUrl = $row['request_url'];
                    $contentType = $row['content_type'];
                    $responseStatus = $row['response_status'];

                    echo "<tr>
                            <td>$ipAddress</td>
                            <td>$requestTime</td>
                            <td>$requestUrl</td>
                            <td>$contentType</td>
                            <td>$responseStatus</td>
                          </tr>";
                }

                echo "</table>";
                echo "</div>";
            } else {
                echo "<p>今天还没有请求日志。</p>";
            }

            // 关闭数据库连接
            $conn->close();
            ?>
        </div>
        <div class="footer">© 2024 API 请求日志查询系统</div>
    </div>
</body>
</html>
